<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Interfaces\ChUserAuthServiceInterface;
use App\CustHub\Request\LoginRequest;
use App\CustHub\Vo\ChUserServiceVo;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: 'ch/login')]
class LoginController extends BaseController
{
    #[Inject]
    protected ChUserAuthServiceInterface $authService;

    /**
     * 手机号密码登录
     * @param LoginRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('passwordLogin')]
    public function passwordLogin(LoginRequest $request): ResponseInterface
    {
        $requestData = $request->validated();
        $vo = new ChUserServiceVo();
        $vo->setAccount($requestData['account']);
        $vo->setPassword($requestData['password']);
        return $this->success($this->authService->passwordLogin($vo));
    }

    /**
     * 短信验证码登录
     * @param LoginRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('mobileLogin')]
    public function mobileLogin(LoginRequest $request): ResponseInterface
    {
        $requestData = $request->all();
        $vo = new ChUserServiceVo();
        $vo->setMobile($requestData['mobile']);
        $vo->setCode($requestData['code']);
        $vo->setInviteCode($requestData['invite_code'] ?? '');
        return $this->success($this->authService->mobileLogin($vo));
    }

    /**
     * 注册账号
     * @param LoginRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('register')]
    public function register(LoginRequest $request): ResponseInterface
    {
        $requestData = $request->validated();
        $vo = new ChUserServiceVo();
        $vo->setMobile($requestData['mobile']);
        $vo->setCode($requestData['code']);
        $vo->setPassword($requestData['password']);
        $this->authService->register($vo);
        return $this->success('注册成功');
    }

    /**
     * 检测邀请码是否需要
     * @param LoginRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('checkInviteCodeNeed')]
    public function checkInviteCodeNeed(LoginRequest $request): ResponseInterface
    {
        return $this->success(['invite_code_need' => $this->authService->checkInviteCodeNeed($request->input('mobile'))]);
    }

    /**
     * 退出登录
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("logout"), ApiAuth]
    public function logout(): ResponseInterface
    {
        $this->authService->logout();
        return $this->success('退出成功');
    }

}